//
//  TYZBaseNavigationController.m
//  TYZKit
//
//  Created by TangBin on 2018/8/23.
//  Copyright © 2018年 TangBin. All rights reserved.
//

#import "TYZBaseNavigationController.h"
//#import "UIColor+TYZAdd.h"
//#import "UIImage+TYZAdd.h"


@implementation TYZNavInfo
- (instancetype)initWithNavBackgroundColor:(UIColor *)bgColor titleFont:(UIFont *)titleFont titleColor:(UIColor *)titleColor
{
    if (self = [super init])
    {
        _bgColor = bgColor;
        _titleFont = titleFont;
        _titleColor = titleColor;
    }
    return self;
}

@end

@interface TYZBaseNavigationController ()

@end

@implementation TYZBaseNavigationController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.interactivePopGestureRecognizer.delegate = self;
    self.delegate = self;
    
    self.interfaceOrientation = UIInterfaceOrientationPortrait;
    self.interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupNavCommonInfo:(TYZNavInfo *)navInfo
{
    if (@available(iOS 15.0, *))
    {
        UINavigationBarAppearance *nav = [UINavigationBarAppearance new];
        [nav configureWithOpaqueBackground]; // 重置背景和阴影颜色
        // 背景色
        nav.backgroundColor = navInfo.bgColor;
        
        nav.shadowImage = [[UIImage alloc] init];
        
        // 去掉半透明效果
        nav.backgroundEffect = nil;
        
        // 去掉导航栏阴影（如果不设置clear，导航栏底下会有一条阴影线）
        nav.shadowColor = [UIColor clearColor];
        
        nav.titleTextAttributes = @{NSForegroundColorAttributeName:navInfo.titleColor, NSFontAttributeName:navInfo.titleFont};
        self.navigationBar.scrollEdgeAppearance = nav; // 带scroll滑动的页面
        self.navigationBar.standardAppearance = nav; // 常规页面
        
        return;
    }
    
    // 导航栏背景
    [self.navigationBar setBarTintColor:navInfo.bgColor];
    
    // navBar title Color font
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:navInfo.titleColor, NSFontAttributeName:navInfo.titleFont}];
    
    // 去除自带的顶部阴影
    self.navigationBar.shadowImage = [[UIImage alloc] init];

    // 设置导航栏控制器颜色
    [self.navigationBar setBackgroundImage:[UIImage imageWithColor:navInfo.bgColor] forBarMetrics:UIBarMetricsDefault];
    
    if ([self.navigationBar respondsToSelector:@selector(setTranslucent:)])
    {
        // 设置导航栏是否具有透明度这个功能
        // 在iOS7以后，默认为YES。{控件从（0，0）点开始，设置为NO,则从nav底部（0，64）开始}；
//        self.navigationBar.translucent = NO;
    }
    
}

- (NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated
{
    if (self.viewControllers.count > 1)
    {
        self.topViewController.hidesBottomBarWhenPushed = NO;
    }
    
    NSArray<__kindof UIViewController *> *viewControllers = [super popToRootViewControllerAnimated:animated];
    return viewControllers;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count > 0)
    {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

#pragma mark - UINavigationControllerDelegate
// 显示Root view controller时，关闭掉interactivePopGestureRecognizer这个手势
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (![navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        return;
    }
    if (viewController == navigationController.viewControllers[0])
    {
        navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    else
    {
        // 允许滑动左边进行pop
        navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 如果在红色方框内有长按手势，这里需要快速返回NO，要不然反应会很迟钝
    return YES;
}

/*
#pragma mark - orientation
// 当前的导航控制器是否可以旋转
- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

// 设置支持的屏幕旋转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return self.interfaceOrientationMask;
}

// 设置presentation方向展示的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return self.interfaceOrientation;
}
*/
#pragma mark - orientation
// 当前的导航控制器是否可以旋转
- (BOOL)prefersStatusBarHidden
{
    if(self.topViewController.prefersStatusBarHidden)
    {
        return self.topViewController.prefersStatusBarHidden;
    }
    return NO;
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
    if (self.topViewController.preferredStatusBarUpdateAnimation)
    {
           return self.topViewController.preferredStatusBarUpdateAnimation;
    }
    return UIStatusBarAnimationNone;
}
- (BOOL)shouldAutorotate
{
    if (self.topViewController.shouldAutorotate)
    {
        return self.topViewController.shouldAutorotate;
    }
    return YES;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

// 设置支持的屏幕旋转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if (self.topViewController.supportedInterfaceOrientations)
    {
        return self.topViewController.supportedInterfaceOrientations;
    }
    return self.interfaceOrientationMask;
}

// 设置presentation方向展示的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if (self.topViewController.preferredInterfaceOrientationForPresentation)
    {
        return self.topViewController.preferredInterfaceOrientationForPresentation;
    }
    return self.interfaceOrientation;
}

@end
